﻿
Imports std

Public Class bytesVector(Of T, T2 As bytesbase(Of T))
    Inherits vector(Of T)
    Implements equalBytes

    Private Shared ReadOnly _t As Type = Nothing

    Shared Sub New()
        _t = GetType(T2)
    End Sub

    Public Sub frombytes(ByVal b As bytes) Implements frombytes.frombytes
        Dim bs() As fromBytes = Nothing
        If deserialize(b, _t, bs) AndAlso Not bs Is Nothing Then
            Dim i As Int64 = 0
            For i = 0 To bs.Length() - 1
                If Not push_back(bs(i).as(Of T)()) Then
                    raiseError(errorHandle.errorType.warning, "cannot push_back")
                    Exit For
                End If
            Next
        End If
    End Sub

    Public Function tobytes() As bytes Implements tobytes.tobytes
        Dim rtn As bytes = Nothing
        Dim bs() As T2 = Nothing
        ReDim bs(size() - 1)
        For i As Int64 = 0 To size() - 1
            bs(i) = alloc(_t)
            bs(i).attach(at(i))
        Next
        If serialize(rtn, False, bs) Then
            Return rtn
        Else
            Return Nothing
        End If
    End Function
End Class
